package com.ysten.basic.common.log.parser;

import com.ysten.basic.common.log.util.OperationType;
import com.ysten.basic.common.log.util.PropertyLogConstants;

 

public abstract class HqlParser
{
    private String sourceSql;
    
    protected HqlSegment segment;
    
    protected abstract void initializeSegments();
    
    protected abstract OperationType getOperateType();
    
    public HqlParser(String sourceSql)
    {
        this.sourceSql = sourceSql;
        initializeSegments();
        segment.parse(sourceSql);
    }
    
    public HqlContent getSqlContent()
    {
        HqlContent content = new HqlContent();
        content.setOperateType(getOperateType());
        content.setComment(segment.getCommnet());
        return content;
    }
    
    public String getSourceSql()
    {
        return sourceSql;
    }
    
    static class DeleteSqlParser extends HqlParser
    {
        public DeleteSqlParser(String sourceSql)
        {
            super(sourceSql);
        }
        
        @Override
        protected void initializeSegments()
        {
            segment = new HqlSegment("(where)(.+)(" + PropertyLogConstants.END_OF_HQL + ")");
        }
        
        @Override
        protected OperationType getOperateType()
        {
            return OperationType.DELETE;
        }
    }
    
    static class UpdateSqlParser extends HqlParser
    {
        public UpdateSqlParser(String sourceSql)
        {
            super(sourceSql);
        }
        
        @Override
        protected void initializeSegments()
        {
            segment = new HqlSegment("(set)(.+)( where |" + PropertyLogConstants.END_OF_HQL + ")");
        }
        
        @Override
        protected OperationType getOperateType()
        {
            return OperationType.UPDATE;
        }
    }
    
    static class InsertSqlParser extends HqlParser
    {
        public InsertSqlParser(String sourceSql)
        {
            super(sourceSql);
        }
        
        @Override
        protected void initializeSegments()
        {
        }
        
        @Override
        public HqlContent getSqlContent()
        {
            return new HqlContent();
        }
        
        @Override
        protected OperationType getOperateType()
        {
            return OperationType.CREATE;
        }
    }
}
